# coding=utf-8
import sys
import os
import re
import numpy as np


def parse(files):
    scores = []
    all_data = []
    for fname in files:
        line = open(fname).readline()
        data = line.strip().split(',')
        data_items = list(map(lambda x: float(x.split('=')[1]), data))
        all_data.append(data_items)

        epoch = re.findall(r'(\d{1,2})hyp', fname)[0]
        scores.append((epoch, line.strip()))
    all_data = np.asarray(all_data)
    mean = np.mean(all_data, axis=0)
    std = np.std(all_data, axis=0)
    mean_std = []
    for i in range(len(mean)):
        mean_std.append(str(mean[i]))
        mean_std.append(str(std[i]))

    scores = sorted(scores, key=lambda x: int(x[0]))
    scores.append([str(len(scores))] + mean_std)
    return scores


input_paths = sys.argv[1:]
distinct_file = map(lambda x: x + '.emb_metrics', input_paths)

# get 2
dfile = distinct_file
uscores = parse(dfile)
with open(input_paths[0] + '.all_emb', 'w') as f:
    uscores = map(lambda x: ','.join(x), uscores)
    f.write('\n'.join(uscores))


